/*
 * Copyright (C) 2008 The Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yalantis.ucrop.util;


import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.render.PixelMapHolder;
import ohos.app.Context;
import ohos.media.image.PixelMap;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;

//尝试使用Image替代功能
public class FastBitmapDrawable extends Component implements Component.DrawTask {

    private PixelMap mBitmap;
    private final int mAlpha;

    public FastBitmapDrawable(Context context, PixelMap b) {
        super(context);
        setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT));
        mAlpha = 255;
        setBitmap(b);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (mBitmap != null && !mBitmap.isReleased()) {
            PixelMapHolder holder = new PixelMapHolder(mBitmap);
            LogUtils.LogInfo("qqqqqqqq:","onDraw");
            canvas.drawPixelMapHolder(holder, 0, 0, new Paint());
        }
    }


//    @Override
//    public void draw(Canvas canvas) {
//        if (mBitmap != null && !mBitmap.isReleased()) {
//            canvas.drawBitmap(mBitmap, null, getBounds(), mPaint);
//        }
//    }

    public float getAlpha() {
        return mAlpha;
    }

    public PixelMap getBitmap() {
        return mBitmap;
    }

    public void setBitmap(PixelMap b) {
//        setPixelMap(b);
        addDrawTask((component, canvas) -> {
            //可以不要？
//                if (mBitmap != null && !mBitmap.isReleased()) {
//                    PixelMapHolder holder = new PixelMapHolder(mBitmap);
//                    LogUtils.LogInfo("qqqqqqqq:","onDraw");
//                    canvas.save();
//                    canvas.drawPixelMapHolder(holder, 0, 0, new Paint());
//                    canvas.restore();
//
//                }
        });
        mBitmap = b;
        int mWidth;
        int mHeight;
        if (b != null) {
            mWidth = mBitmap.getImageInfo().size.width;
            mHeight = mBitmap.getImageInfo().size.height;
            LogUtils.LogInfo("qqqqqq:","setBitmap:bitmap size:" + mHeight + " x " + mWidth);
        } else {
            mWidth = mHeight = 0;
            System.out.println(mHeight);
            System.out.println(mWidth);
        }
    }

}
